﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using MpSoft.Library.DTO;
using MpSoft.Basic.DataAccess;

namespace MpSoft.Library.DAO.Customers
{
    /// <summary>
    /// This object represents the methods of a Contact.
    /// </summary>
    internal class ContactDAO
    {

        #region general
        private static Contact LoadObj(IDataReader datareader)
        {
            Contact obj = new Contact();
            obj.ContactID = MpDataHelp.GetInt(datareader, "ContactID");
            obj.Fullname = MpDataHelp.GetString(datareader, "Fullname");
            obj.Title = MpDataHelp.GetString(datareader, "Title");
            obj.Company = MpDataHelp.GetString(datareader, "Company");
            obj.PhoneNumber = MpDataHelp.GetString(datareader, "PhoneNumber");
            obj.Email = MpDataHelp.GetString(datareader, "Email");
            obj.Address = MpDataHelp.GetString(datareader, "Address");
            obj.Content = MpDataHelp.GetString(datareader, "Content");
            obj.DateCreate = MpDataHelp.GetDateTime(datareader, "DateCreate");

            return obj;
        }
        #endregion

        public static int Contact_Add(Contact obj)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Contact_Add");
            db.AddOutParameter(dbCommand, "ContactID", DbType.Int32, obj.ContactID);
            db.AddInParameter(dbCommand, "Fullname", DbType.String, obj.Fullname);
            db.AddInParameter(dbCommand, "Title", DbType.String, obj.Title);
            db.AddInParameter(dbCommand, "Company", DbType.String, obj.Company);
            db.AddInParameter(dbCommand, "PhoneNumber", DbType.String, obj.PhoneNumber);
            db.AddInParameter(dbCommand, "Email", DbType.String, obj.Email);
            db.AddInParameter(dbCommand, "Address", DbType.String, obj.Address);
            db.AddInParameter(dbCommand, "Content", DbType.String, obj.Content);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return Convert.ToInt32(db.GetParameterValue(dbCommand, "@ContactID"));
            return -1;
        }
        public static int Contact_Update(Contact obj)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Contact_Update");
            db.AddInParameter(dbCommand, "ContactID", DbType.Int32, obj.ContactID);
            db.AddInParameter(dbCommand, "Fullname", DbType.String, obj.Fullname);
            db.AddInParameter(dbCommand, "Title", DbType.String, obj.Title);
            db.AddInParameter(dbCommand, "Company", DbType.String, obj.Company);
            db.AddInParameter(dbCommand, "PhoneNumber", DbType.String, obj.PhoneNumber);
            db.AddInParameter(dbCommand, "Email", DbType.String, obj.Email);
            db.AddInParameter(dbCommand, "Address", DbType.String, obj.Address);
            db.AddInParameter(dbCommand, "Content", DbType.String, obj.Content);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return 1;
            return -1;
        }

        public static int Contact_Delete(Int32 ContactID)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Contact_Delete");
            db.AddInParameter(dbCommand, "ContactID", DbType.Int32, ContactID);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return 1;
            return -1;
        }

        public static List<Contact> Contact_List(int ContactID, string Email)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Contact_List");
            db.AddInParameter(dbCommand, "ContactID", DbType.Int32, ContactID);
            db.AddInParameter(dbCommand, "Email", DbType.String, Email);
            List<Contact> listObj = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    (listObj = new List<Contact>()).Add(LoadObj(dataReader));
                    while (dataReader.Read())
                        listObj.Add(LoadObj(dataReader));
                }
            }
            return listObj;
        }
    }
}



